package com.miracle.main.module.mvp.ui.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.view.View;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.miracle.basemvp.base.BaseActivity;
import com.miracle.basemvp.base.BaseFragment;
import com.miracle.basemvp.di.component.AppComponent;
import com.miracle.main.R;
import com.miracle.main.R2;
import com.miracle.main.module.di.component.DaggerFileComponent;
import com.miracle.main.module.di.module.FileModule;
import com.miracle.main.module.mvp.contract.FileContract;
import com.miracle.main.module.mvp.presenter.FilePresenter;
import com.miracle.main.module.mvp.ui.activity.UploadActivity;
import com.trello.rxlifecycle2.LifecycleProvider;
import com.trello.rxlifecycle2.android.FragmentEvent;

import java.io.File;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * com.miracle.main.module.mvp.ui.fragment
 * (c)2018 AIR Times Inc. All rights reserved.
 *
 *
 * @author WangJQ
 * @version 1.0
 * @date 2018/7/2 18:14
 * @see com.miracle.main.module.mvp.ui.fragment
 */
public class FileFragment extends BaseFragment<FilePresenter> implements FileContract.View,
        TabLayout.OnTabSelectedListener {
    @BindView(R2.id.iv_nav3_icon_edit)
    ImageView mIvEdit;
    @BindView(R2.id.tv_title)
    TextView mTvTitle;
    @BindView(R2.id.tab)
    TabLayout mTabLayout;

    LifecycleProvider<FragmentEvent> lifecycleProvider;

    int mContainerId = R.id.fl_file;

    FileListFragment mAllListFragment;
    FileListFragment mPicListFragment;
    FileListFragment mDocListFragment;
    FileListFragment mNoteListFragment;

    @Override
    public int setLayoutId(@Nullable Bundle bundle) {
        return R.layout.cloud_fragment_file;
    }

    @Override
    public void setDaggerComponent(AppComponent appComponent) {
        DaggerFileComponent
                .builder()
                .appComponent(appComponent)
                .fileModule(new FileModule(this))
                .build()
                .inject(this);
    }

    @Override
    public void init(@Nullable Bundle bundle) {
        mTvTitle.setText("文件中心");
        mIvEdit.setImageResource(R.drawable.icon_add_2);
    }

    @Override
    public void initView() {
        mTabLayout.addTab(mTabLayout.newTab().setText("全部"));
        mTabLayout.addTab(mTabLayout.newTab().setText("图片"));
        mTabLayout.addTab(mTabLayout.newTab().setText("文档"));
        mTabLayout.addTab(mTabLayout.newTab().setText("笔记"));
        mTabLayout.addOnTabSelectedListener(this);
        onTabSelected(mTabLayout.getTabAt(0));
        initFragment();
    }

    private void initFragment() {

    }

    @Override
    public void initData() {
    }

    @Override
    public void onRequest(LifecycleProvider<FragmentEvent> lifecycleProvider) {
        this.lifecycleProvider = lifecycleProvider;
    }

    @Override
    public void showData(Object o) {

    }

    @OnClick(R2.id.iv_nav3_icon_edit)
    public void onViewClicked(View view) {
        int id = view.getId();
        if (id == R.id.iv_nav3_icon_edit) {
            UploadActivity.start((BaseActivity) mActivity);
//            testSubmitFile();
//            showAddPopup();
        }
    }

    private void testSubmitFile() {

//        mPresenter.uploadFiles(lifecycleProvider);
    }
    PopupWindow mAddPopup;
    private void showAddPopup(){
        View view = mActivity.getLayoutInflater().inflate(R.layout.dialog_add,
                null);
        mAddPopup = new PopupWindow(view);
        mAddPopup.showAsDropDown(mIvEdit);
    }

    @Override
    public void onTabSelected(TabLayout.Tab tab) {
        switch (tab.getPosition()) {
            case 0:
                if (mAllListFragment == null) {
                    mAllListFragment = FileListFragment.newInstance(FileListFragment.TYPE_ALL);
                }
                mFragmentController.showOrHideFragment(mContainerId, mAllListFragment);
                break;
            case 2:
                if (mDocListFragment == null) {
                    mDocListFragment = FileListFragment.newInstance(FileListFragment.TYPE_DOC);
                }
                mFragmentController.showOrHideFragment(mContainerId, mDocListFragment);
                break;
            case 1:
                if (mPicListFragment == null) {
                    mPicListFragment = FileListFragment.newInstance(FileListFragment.TYPE_PIC);
                }
                mFragmentController.showOrHideFragment(mContainerId, mPicListFragment);
                break;
            case 3:
                if (mNoteListFragment == null) {
                    mNoteListFragment = FileListFragment.newInstance(FileListFragment.TYPE_NOTE);
                }
                mFragmentController.showOrHideFragment(mContainerId, mNoteListFragment);
                break;
            default:
                break;
        }
    }

    @Override
    public void onTabUnselected(TabLayout.Tab tab) {

    }

    @Override
    public void onTabReselected(TabLayout.Tab tab) {

    }
}
